package com.bl.student.service.biz.chat.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.enums.UserStatus;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.ApplyHistoryEntity;
import com.bl.student.dao.model.TeacherEntity;
import com.bl.student.dao.model.TeacherStudentEntity;
import com.bl.student.dao.model.UserEntity;
import com.bl.student.dao.redis.model.TeacherInfo;
import com.bl.student.dao.redis.model.UserInfo;
import com.bl.student.dao.repository.ApplyHistoryRepository;
import com.bl.student.dao.repository.TeacherRepository;
import com.bl.student.dao.repository.TeacherStudentRepository;
import com.bl.student.dao.repository.UserRepository;
import com.bl.student.service.biz.BaseService;
import com.bl.student.service.biz.chat.ChatService;
import com.bl.student.service.biz.teacher.TeacherService;
import com.bl.student.service.biz.user.UserService;
import com.bl.student.service.vo.ListVo;
import com.bl.student.service.vo.chat.ChangeTeacherVo;
import com.bl.student.service.vo.chat.ChatTeacherVo;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;

/**
 * Created by yujingyi on 2018/3/10.
 */
@Service
@Log4j2
public class ChatServiceImpl extends BaseService implements ChatService {

    static final private long RECOMMEND_WEIGHT = 100;

    static final private String HAS_TEACHER = "您已选择画痨!";

    @Autowired
    TeacherStudentRepository teacherStudentRepository;

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    TeacherService teacherService;

    @Autowired
    UserService userService;

    @Autowired
    UserRepository userRepository;

    @Autowired
    ApplyHistoryRepository applyHistoryRepository;

    @Override
    public ListVo<ChatTeacherVo> getTeacherList(UserInfo userInfo, Pageable pageable) {
        ListVo<ChatTeacherVo> listVo = new ListVo<>();
        List<ChatTeacherVo> chatTeacherListVoList = Lists.newArrayList();
        Page<TeacherEntity> teacherEntityList = teacherRepository.findAllByStatus(UserStatus.NORMAL, pageable);
        listVo.setTotal(teacherEntityList.getTotalElements());
        if (listVo.getTotal() == 0) {
            listVo.setList(chatTeacherListVoList);
        }
        for (TeacherEntity teacherEntity : teacherEntityList.getContent()) {
            ChatTeacherVo chatTeacherVo = new ChatTeacherVo();
            BeanUtils.copyProperties(teacherEntity, chatTeacherVo);
//            chatTeacherVo.setRecommended(teacherEntity.getWeight() >= RECOMMEND_WEIGHT);
            chatTeacherListVoList.add(chatTeacherVo);
        }
        listVo.setList(chatTeacherListVoList);
        return listVo;

    }

    @Override
    @Transactional
    public void enroll(UserInfo userInfo, String teacherAccount) {
        TeacherEntity teacherEntity = teacherService.getTeacherByMobile(teacherAccount);
        if (teacherEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST.getCode(), ApiResponseCode.DATA_NOT_EXIST.getMsg());
        }
        if (userService.getTeacherStudent(userInfo.getId()) != null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), HAS_TEACHER);
        }
        TeacherStudentEntity teacherStudentEntity = new TeacherStudentEntity();
        teacherStudentEntity.setId(UUIDUtils.getUUID());
        teacherStudentEntity.setStudentId(userInfo.getId());
        teacherStudentEntity.setTeacherId(teacherEntity.getId());
        teacherStudentRepository.save(teacherStudentEntity);
    }

    @Override
    @Transactional
    public void change(UserInfo userInfo, ChangeTeacherVo changeTeacherVo) {
        ApplyHistoryEntity applyHistoryEntity = new ApplyHistoryEntity();
        applyHistoryEntity.setId(UUIDUtils.getUUID());
        applyHistoryEntity.setUserId(userInfo.getId());
        applyHistoryEntity.setReason(changeTeacherVo.getReason());
        applyHistoryEntity.setCreateTime(new Date());

        applyHistoryRepository.save(applyHistoryEntity);
    }

    @Transactional
    @Override
    public void spit(TeacherInfo teacherInfo, String studentMobile) {
        UserEntity userEntity = userService.getUserByMobile(studentMobile);
        if (userEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST.getCode(), ApiResponseCode.DATA_NOT_EXIST.getMsg());
        }
    }
}
